package net.huashitong.keyan.web;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.keyan.entity.InspectionSummary;
import net.huashitong.keyan.filter.InspectionSummaryFilter;
import net.huashitong.keyan.service.InspectionSummaryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping(value = "/inspectionSummary")
public class InspectionSummaryController extends JsonBaseController {

    @Autowired
    private InspectionSummaryService service;

    @Override
    protected String getView(String s) {
        return "/modules/keyan/insSum/" + s;
    }

    /**
     * 载入页面
     *
     * @param model
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public String initPage(Model model) {
        return getView("/query");
    }


    /**
     * 初始化数据
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public JqGrid query(@ModelAttribute InspectionSummaryFilter filter) {
        List<InspectionSummary> list = service.query(filter);
        return coverJqGrid(filter, list);
    }


    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String showAdd(Model model) {
        InspectionSummaryFilter filter = new InspectionSummaryFilter();
        model.addAttribute("filter", filter);
        return getView("/form");
    }

    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Message add(@ModelAttribute InspectionSummaryFilter filter) {
        InspectionSummary netIndicators = new InspectionSummary();
        BeanUtils.copyProperties(filter, netIndicators);
        service.save(netIndicators);
        return coverMessage("200", "保存成功！");
    }

    @RequestMapping(value = "/edit/{id}")
    public String showUpdate(Model model, @PathVariable String id) {
        InspectionSummary inspectionSummary = service.getById(id);
        model.addAttribute("filter", inspectionSummary);
        return getView("/form");
    }

    @ResponseBody
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Message update(@ModelAttribute InspectionSummaryFilter filter) {
        InspectionSummary inspectionSummary = new InspectionSummary();
        BeanUtils.copyProperties(filter, inspectionSummary);
        service.update(inspectionSummary);
        return coverMessage("200", "修改成功！");
    }


    @ResponseBody
    @RequestMapping(value = "/delete/{id}")
    public Message delete(@PathVariable List<String> id) {
        service.batchDelete(id);
        return coverMessage("200", "删除成功！");
    }

    @RequestMapping("/filter")
    public String queryFilter(@ModelAttribute InspectionSummaryFilter filter) {
        return getView("/filter");
    }

}
